package config;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.StringTokenizer;

import exceptions.KeyLanguageExc;
import exceptions.PositionLanguage;

public class Language {

	private String schedule;
	private String[] daysOfWeek;
	private char[] abbrDaysOfWeek;
	private String[] monthsOfYear;
	private String[] periods;
	private String createdBy;
	private String[] closeOpen;
	private String error;

	private static final int NUM_DAYS_OF_WEEK = 7;
	private static final int NUM_MONTHS_OF_YEAR = 12;
	private static final int NUM_PERIODS = 4;
	private static final int NUM_STATUS = 2;

	Map<String, String[]> interIn = new LinkedHashMap<String, String[]>();
	Map<String, String[]> interOut = new LinkedHashMap<String, String[]>();

	public Map<String, String[]> getInterIn() {
		return interIn;
	}

	public Map<String, String[]> getInterOut() {
		return interOut;
	}

	public void LoadInternationalIn(String extIn) {

		String nameFileIn = "internacional." + extIn;
		this.daysOfWeek = new String[NUM_DAYS_OF_WEEK];
		String abbrDays;
		this.monthsOfYear = new String[NUM_MONTHS_OF_YEAR];
		this.periods = new String[NUM_PERIODS];
		this.closeOpen = new String[NUM_STATUS];

		try (BufferedReader internacionalIn = new BufferedReader(
				new FileReader(nameFileIn))) {

			String[] valorIn = null;
			String clauIn = null;
			String c, x;

			while ((c = internacionalIn.readLine()) != null) {
				StringTokenizer clave1 = new StringTokenizer(c, ";");
				while (clave1.hasMoreTokens()) {
					clauIn = clave1.nextToken();
					// System.out.println("Clave: " + clau);
					x = clave1.nextToken();
					// System.out.println("Valor cadena: " + x);
					valorIn = x.split(",");
				}
				interIn.put(clauIn, valorIn);
			}
			this.schedule = getStringLangIn("001", 0);
			for (int i = 0; i < daysOfWeek.length; i++) {
				this.daysOfWeek[i] = getStringLangIn("002", i);
			}
			abbrDays = getStringLangIn("003", 0);
			abbrDaysOfWeek = abbrDays.toCharArray();
			for (int i = 0; i < monthsOfYear.length; i++) {
				this.monthsOfYear[i] = getStringLangIn("004", i);
			}
			for (int i = 0; i < periods.length; i++) {
				this.periods[i] = getStringLangIn("005", i);
			}
			this.createdBy = getStringLangIn("006", 0);
			for (int i = 0; i < closeOpen.length; i++) {
				this.closeOpen[i] = getStringLangIn("007", i);
			}
			this.error = getStringLangIn("008", 0);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (PositionLanguage e) {
			e.printStackTrace();
		} catch (KeyLanguageExc e) {
			e.printStackTrace();
		}

	}

	public void LoadInternationalOut(String extOut) {

		String nameFileOut = "internacional." + extOut;
		this.daysOfWeek = new String[NUM_DAYS_OF_WEEK];
		String abbrDays;
		this.monthsOfYear = new String[NUM_MONTHS_OF_YEAR];
		this.periods = new String[NUM_PERIODS];
		this.closeOpen = new String[NUM_STATUS];
		try (BufferedReader internacionalOut = new BufferedReader(
				new FileReader(nameFileOut))) {

			String[] valorOut = null;
			String clauOut = null;
			String d, y;

			while ((d = internacionalOut.readLine()) != null) {
				StringTokenizer clave2 = new StringTokenizer(d, ";");
				while (clave2.hasMoreTokens()) {
					clauOut = clave2.nextToken();
					y = clave2.nextToken();
					valorOut = y.split(",");
				}
				interOut.put(clauOut, valorOut);
			}
			this.schedule = getStringLangOut("001", 0);
			for (int i = 0; i < daysOfWeek.length; i++) {
				this.daysOfWeek[i] = getStringLangOut("002", i);
			}
			abbrDays = getStringLangOut("003", 0);
			abbrDaysOfWeek = abbrDays.toCharArray();
			for (int i = 0; i < monthsOfYear.length; i++) {
				this.monthsOfYear[i] = getStringLangOut("004", i);
			}
			for (int i = 0; i < periods.length; i++) {
				this.periods[i] = getStringLangOut("005", i);
			}
			this.createdBy = getStringLangOut("006", 0);
			for (int i = 0; i < closeOpen.length; i++) {
				this.closeOpen[i] = getStringLangOut("007", i);
			}

			this.error = getStringLangOut("008", 0);

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (PositionLanguage e) {
			e.printStackTrace();
		} catch (KeyLanguageExc e) {
			e.printStackTrace();
		}

	}

	public String getStringLangIn(String clave, int position)
			throws PositionLanguage, KeyLanguageExc {

		String[] recoger = null;
		String clau = null;
		String key = null;
		Iterator<String> it = interIn.keySet().iterator();
		while (it.hasNext()) {
			key = (String) it.next();
			if (clave.equals(key)) {
				recoger = interIn.get(clave);
				clau = key;
			}
		}

		if (clau == null) {
			throw new KeyLanguageExc();
		} else if (recoger.length <= position) {
			throw new PositionLanguage();
		} else {
			String res = new String(recoger[position]);
			return res;
		}

	}

	public String getStringLangOut(String clave, int position)
			throws PositionLanguage, KeyLanguageExc {

		String[] recoger = null;
		String clau = null;
		String key = null;
		Iterator<String> it = interOut.keySet().iterator();
		while (it.hasNext()) {
			key = (String) it.next();
			if (clave.equals(key)) {
				recoger = interOut.get(clave);
				clau = key;
			}
		}
		if (clau == null) {
			throw new KeyLanguageExc();
		} else if (recoger.length <= position) {
			throw new PositionLanguage();
		} else {
			String res = new String(recoger[position]);
			return res;
		}

	}

	public String getSchedule() {
		return schedule;
	}

	public String[] getDaysOfWeek() {
		return daysOfWeek;
	}

	public char[] getAbbrDaysOfWeek() {
		return abbrDaysOfWeek;
	}

	public String[] getMonthsOfYear() {
		return monthsOfYear;
	}

	public String[] getPeriods() {
		return periods;
	}

	public String getCreatedBy() {
		return createdBy;
	}

	public String[] getCloseOpen() {
		return closeOpen;
	}

	public String getError() {
		return error;
	}
}